﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;

namespace katOS
{
    class Interpreter
    {

        public void To_Excecute(Process new_process) 
        {
            string file_name = "C:/Users/kareAndy/Documents/UVG/Ciclo 5/CC3002 Sistemas Operativos/Proyecto No. 1/katOSFiles/" + new_process.getFileName() + ".txt";
            Parser a_parser = new Parser(new_process, new_process.getFileName());
            LinkedList<String> instruction_set = new_process.getInstructions();
            UserPrograms a_program = new UserPrograms();
            String[] part_instruction;

            if (instruction_set != null)
            {
                for (int i = 0; i < instruction_set.Count; i++)
                {

                    part_instruction = instruction_set.ElementAt(i).Split(' ');
                    int tempip = new_process.getIP();
                    switch (part_instruction[0])
                    {
                        case "set":
                            if (part_instruction.Length == 3)
                            {
                                a_program.Set(new_process, part_instruction[1], part_instruction[2]);
                            }
                            else
                            {
                                Console.WriteLine("Wrong parameters for a Set instruction");
                            }
                            new_process.setIP(tempip++);
                            break;

                        case "add":
                            if (part_instruction.Length == 4)
                            {
                                a_program.Add(new_process, part_instruction[1], part_instruction[2], part_instruction[3]);
                            }
                            else
                            {
                                Console.WriteLine("Wrong parameters for an Add instruction");
                            }
                            new_process.setIP(tempip++);
                            break;

                        case "sub":
                            if (part_instruction.Length == 4)
                            {
                                a_program.Sub(new_process, part_instruction[1], part_instruction[2], part_instruction[3]);
                            }
                            else
                            {
                                Console.WriteLine("Wrong parameters for a Sub instruction");
                            }
                            new_process.setIP(tempip++);
                            break;

                        case "mul":
                            if (part_instruction.Length == 4)
                            {
                                a_program.Mul(new_process, part_instruction[1], part_instruction[2], part_instruction[3]);
                            }
                            else
                            {
                                Console.WriteLine("Wrong parameters for a Mul instruction");
                            }
                            new_process.setIP(tempip++);
                            break;

                        case "div":
                            if (part_instruction.Length == 5)
                            {
                                a_program.Div(new_process, part_instruction[1], part_instruction[2], part_instruction[3], part_instruction[4]);
                            }
                            else
                            {
                                Console.WriteLine("Wrong parameters for a Div instruction");
                            }
                            new_process.setIP(tempip++);
                            break;

                        case "jmp":
                            if (part_instruction.Length == 3)
                            {
                                a_program.Jmp(new_process, part_instruction[1], part_instruction[2]);
                            }
                            else
                            {
                                Console.WriteLine("Wrong parameters for a Jmp instruction");
                            }
                            new_process.setIP(tempip++);
                            break;

                        case "echo":
                            if (instruction_set.ElementAt(i).Length >= 5)
                            {
                                string a_string = instruction_set.ElementAt(i).Substring(5);
                                a_program.Echo(new_process, a_string);
                            }
                            else
                            {
                                Console.WriteLine("Wrong parameters for an Echo instruction");
                            }
                            new_process.setIP(tempip++);
                            break;

                        case "openfile":
                            Console.WriteLine("OpenFile");
                            //call OpenFile method
                            new_process.setIP(tempip++);
                            break;

                        case "closefile":
                            Console.WriteLine("CloseFile");
                            //call CloseFile method
                            new_process.setIP(tempip++);
                            break;

                        case "readline":
                            Console.WriteLine("ReadLine");
                            //call ReadLine method
                            new_process.setIP(tempip++);
                            break;

                        case "sleep":
                            Console.WriteLine("sleep");
                            //call Sleep method
                            new_process.setIP(tempip++);
                            break;

                        case "end":
                            Console.WriteLine("End");
                            //new_process.setState();
                            new_process.setIP(tempip++);
                            break;

                        case "fork":
                            Console.WriteLine("Fork");
                            //call Fork method
                            new_process.setIP(tempip++);
                            break;

                        case "getstate":
                            Console.WriteLine("GetState");
                            //call GetState method
                            new_process.setIP(tempip++);
                            break;

                        default:
                            break;
                    }
                }
            }
        }
    }
}
